// pages/my/myInfo_admin_security_code_set/myInfo_admin_security_code_set.js
const app = getApp();
const API = require('../../../API/api.js');
import Notify from '../../../utils/vant-weapp/notify/notify';

const util = require('../../../utils/util.js')
Page({
    bindCodeInput(e) {
        this.setData({
            code: e.detail.value
        })
    },
    getCode(e) {
        console.log('获取验证码');
        this.timer();
    },
    timer: function () {
        let promise = new Promise((resolve, reject) => {
            let setTimer = setInterval(
                () => {
                    var second = this.data.second - 1;
                    this.setData({
                        second: second,
                        btnValue: second + '秒',
                        btnDisabled: true
                    })
                    if (this.data.second <= 0) {
                        this.setData({
                            second: 60,
                            btnValue: '获取验证码',
                            btnDisabled: false
                        })
                        resolve(setTimer)
                    }
                }, 1000)
        })
        promise.then((setTimer) => {
            clearInterval(setTimer)
        })
    },
    next() {
        console.log(this.data.code);
        // wx.navigateTo({
        //   url: '../../my/myInfo_admin_security_code_set_password1/myInfo_admin_security_code_set_password1',

        // })
        var len = this.data.code.length;
        if (len != 6) {
            return Notify('请输入正确的6位验证码')
        }
        //实名验证
        console.log(this.data.pageId)
        if (this.data.pageId == 1) {
            API.addRealNameInfo({
                userId: wx.getStorageSync('userId'),
                name: JSON.parse(wx.getStorageSync('realNameInfo')).name,
                idCard: JSON.parse(wx.getStorageSync('realNameInfo')).idcard,
                verificationCode: this.data.code
            }).then((res) => {
                console.log(res)
                if (res.code == 1) {
                    wx.navigateTo({
                        url: '../../my/myInfo_admin_info_verify_pass/myInfo_admin_info_verify_pass',
                    })
                } else {
                    Notify('验证码错误');
                }
            })
        } else if (this.data.pageId == 2) {
            //安全码验证
            API.setSecurityCode({
                userId: wx.getStorageSync('userId'),
                securityCode: wx.getStorageSync('securityCode'),
                verificationCode: this.data.code
            }).then((res) => {
                console.log(res)
                if (res.code == 1) {
                    wx.redirectTo({
                        url: '../../my/myInfo_admin_security_code/myInfo_admin_security_code',
                    })
                } else {
                    Notify('验证码错误');
                }
            })
        } else if (this.data.pageId == 3) {
            API.settingAPlay({
                userId: wx.getStorageSync('userId'),
                payNum: wx.getStorageSync('aplayAdmin'),
                verificationCode: this.data.code
            }).then((res) => {
                console.log(res)
                if (res.code == 1) {
                    wx.redirectTo({
                        url: '../../my/myInfo_wallet_depositAccount/myInfo_wallet_depositAccount',
                    })
                } else {
                    Notify('验证码错误');
                }
            })
        } else if (this.data.pageId == 4) {
            API.getMoeny({
                userId: wx.getStorageSync('userId'),
                moneny: wx.getStorageSync('getMoney'),
                verificationCode: this.data.code
            }).then((res) => {
                console.log(res)
                if (res.code == 1) {
                    wx.redirectTo({
                        url: '../../my/myInfo_wallet_passDeposit/myInfo_wallet_passDeposit',
                    })
                } else {
                    Notify('验证码错误');
                }
            })
        }
    },
    /**
     * 页面的初始数据
     */
    data: {
        hidden: true,
        btnValue: '',
        btnDisabled: false,
        code: '',
        second: 60,
        mobile: '',
        pageId: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.pageId)
        if (options.pageId) {
            this.setData({
                pageId: options.pageId
            })
        }
        API.getMyUserInfo({
            userId: wx.getStorageSync('userId')
        }).then((res) => {
            this.setData({
                mobile: util.plusxing(res.data.mobile, 3, 4)
            })
        })
        let val = 1111
        if (val != '') {
            this.setData({
                hidden: false,
                btnValue: '获取验证码'
            })
        } else {
            this.setData({
                hidden: true
            })
        }
        this.getCode()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
